/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package net.sf.l2j.gameserver.custom;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

import net.sf.l2j.Config;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.serverpackets.ExShowScreenMessage;

/**
 * @author Anarchy
 *
 */
public class VoteReward implements Runnable
{
	public static boolean ACTIVE = false;
	
	private L2PcInstance _voter;
	private int hzVotes = 0,
		tzVotes = 0;
	
	public VoteReward(L2PcInstance voter)
	{
		_voter = voter;
		
		getVotesHz();
		getVotesTz();
	}
	
	@Override
	public void run()
	{
		int HzVotes = hzVotes, TzVotes = tzVotes;	
		getVotesHz();
		getVotesTz();
		
		if (HzVotes != hzVotes && TzVotes != tzVotes)
		{
			_voter.sendMessage("You have been rewarded. You can vote again in 12 hours.");
			ExShowScreenMessage d = new ExShowScreenMessage("You have been rewarded. You can vote again in 12 hours.", 5000);
			_voter.sendPacket(d);
			_voter.setLastVoteTime(System.currentTimeMillis());
			for (int i : Config.VOTE_SYSTEM_REWARDS.keySet())
			{
				_voter.addItem("VR", i, Config.VOTE_SYSTEM_REWARDS.get(i), _voter, true);
			}
		}
		else
		{
			ExShowScreenMessage d = new ExShowScreenMessage("You didn't successfully vote on both pages. You can vote again in 12 hours.", 5000);
			_voter.sendPacket(d);
			_voter.sendMessage("You didn't successfully vote on both pages. You were not rewarded.");
			_voter.sendMessage("You can vote again in 12 hours.");
			_voter.setLastVoteTime(System.currentTimeMillis());
		}
	}
	
	private void getVotesHz()
	{
		InputStreamReader isr = null;
		BufferedReader br = null;
		
		try
		{
			URLConnection con = new URL(Config.VOTE_SYSTEM_HOPZONE_LINK).openConnection();
			con.addRequestProperty("User-Agent", "Mozilla/4.76");
			isr = new InputStreamReader(con.getInputStream());
			br = new BufferedReader(isr);
			
			String line;
			while ((line = br.readLine()) != null)
			{
				if (line.contains("rank anonymous tooltip"))
				{
					int votes = Integer.valueOf(line.split(">")[2].replace("</span", ""));
					hzVotes = votes;
				}
			}
			br.close();
			isr.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
			System.out.println("Error while getting server vote count.");
		}
	}
	
	private void getVotesTz()
	{
		InputStreamReader isr = null;
		BufferedReader br = null;
		
		try
		{
			URLConnection con = new URL(Config.VOTE_SYSTEM_TOPZONE_LINK).openConnection();
			con.addRequestProperty("User-Agent", "Mozilla/4.76");
			isr = new InputStreamReader(con.getInputStream());
			br = new BufferedReader(isr);
			
			boolean got = false;
			
			String line;
			while ((line = br.readLine()) != null)
			{
				if (line.contains("<tr><td><div align=\"center\"><b><font style=\"font-size:14px;color:#018BC1;\"") && !got)
				{
					got = true;
					int votes = Integer.valueOf(line.split("=\"font-size:14px;color:#018BC1;\">")[1].replace("</font></b></div></td></tr>", ""));
					tzVotes = votes;
				}
			}
			br.close();
			isr.close();
		}
		catch (Exception e)
		{
			System.out.println(e);
			System.out.println("Error while getting server vote count.");
		}
	}
}
